IF Statement bug

Hello,

I tried to find out on the forum a subject regarding a bug I've been informed in DOORS DXL but found nothing.

I would like to talk about this "bug" and see if other users met it and if it still exists in new DOORS release.

Indeed, in DOORS 7.1 and 8.3, whenever an if statement is preceeded by N tabulation(s) and the line after is a comment preceeded by at least N tabulation(s), then the statement is always true.




if (

false) 
// override if 
{ print 
"We don't have the bug!!!\n" 
}   

if (

false) 
// override if 
{ print 
"We have the bug\n" 
}   

if (

false) 
// override if 
{ print 
"We have the bug\n" 
}


Do you know if this bug still exists in DOORS 9.0+?

Best regards,
MattMar
SystemAdmin - Mon Nov 02 09:29:14 EST 2009

Re: IF Statement bug
Peter_Albert - Mon Nov 02 10:14:56 EST 2009

Well, I would not necessarily call this a bug.

From the DXL help:

"The if statement takes an expression of type bool, which must be in parenthesis. If the expression evaluates to true, it executes the following statement, which can be a block."

Can be a block (surrounded by curly braces), but does not have to.

Then, for comments:

"The characters // start a comment which terminates at the end of the line on which it occurs."

So, DXL correctly interprets your code. In the second 'if (false)' example, the statement to be executed in the 'false' case is "tabulator, followed by a comment". Well, this is a strange statement, admittedly, but it is valid.

The following compound statement (The 'print "we have the bug"') is just executed in any case, regardless of the if clause.

Although this is in my view correct, it is of course easily leading to mistakes. Which is why I have taken the habit to comment if clauses as follows:




if (expression) 
{ 
// This is what happens when the expression is true some statements 
} 
// This is what happens when the expression is true 

else 
{ 
// This is what happens when the expression is false some statements 
} 
// This is what happens when the expression is false


Repeating the comment at the opening and closing brackets has become quite useful in longer scripts.

Regards,

Peter

Re: IF Statement bug
llandale - Mon Nov 02 11:13:31 EST 2009

Yes, <tab> is a valid statement and is getting bypassed by the if statement. Then the braced block executes. Yes, put the comments for a block inside the block. If the comment is about the if statement, then put it before the if.

You can see it more clearly if you inserted an 'else' line before each of your 3 openning braces. Doing that, you get the 'no error' print statement, no doubt DXL figured out that there is no 'then' clause and jumps right to the else.

  • Louie